<?php

namespace app\api\controller;
use app\api\model\Member;
use think\facade\Filesystem;
class Account
{
    public $memberData;
    public function __construct()
    {
        //判断token是否有效，通过token读取学生的所有信息
        $token = input('get.token');
        $tokenData = Member::where('member_token', '=', $token)->find();
        if($tokenData == 1) output_error('token无效');
        $allStudent = array();
        $allStudent[] = ['get.member_mobile'];
        $allStudent[] = ['get.member_name'];
        $allStudent[] = ['get.member_gender'];
        $allStudent[] = ['get.member_avatar'];
        $allStudent[] = ['get.memeber_id'];
        $datas = Member::where($allStudent)->field();
        output_data($datas);
    }

    //注册功能
    public function getData()
    {
        //输出信息，去除“member_password”字段
        unset($this->memberData['member_password']);
    }

    public function modifyName()
    {
        //修改学生姓名，学生姓名可重复。
        $id = input('get.id');
        $newName = input('get.newsname');
        if (empty($newName)) output_error('姓名不能为空');
        Member::where('member_id', '=', $id)->update(array('member_name' => $newName));
    }

    public function modifyAvatar()
    {
        //修改学生头像，需要自行创建文件夹，文件系统。
        $file = request()->file('image');
        $savename = Filesystem::disk('avatar')->putFile('', $file, 'uniqid');
        //学生头像文件唯一，更新时需要删除原有的文件。
        if ($this->memberData['member_avatar'] != 'default.png') {
            unlink(app()->getRootPath() . 'public\\upload\\image\\avatar\\' . $this->memberData['member_avatar']);
        }
        //第三步：更新用户的头像字段
        Member::where('member_id', '=', $this->memberId)->update(array('member_avatar' => $savename));

    }

}
